﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using UDP;

namespace XDSlaverNetPlayer
{
    class XDUdpSocket
    {
        private Slaver_Form parent;
        private Server udpSock = null;
       int port = 8002;
       public string recvString;
       public string strRemoteIP;

       public XDUdpSocket(Slaver_Form form)
       {
           parent = form;

       }
       public int nRemotePort;
  
       public bool Create(int port)
       {
           this.port = port;
           udpSock = new UDP.Server(new IPEndPoint(IPAddress.Any, port));
           if (udpSock == null)
               return false;
           udpSock.RecvDatagram += new UDP.NetEventArgsHandler(UDP_RecvDatagram);
           return true;
       }
        public bool Send(string data)
        {
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(data);
            IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(strRemoteIP), nRemotePort);
            try
            {
                udpSock.SendTo(bytes, endpoint);
                return true;
            }
            catch (Exception err)
            {
                return false;
            }
        }
        protected void UDP_RecvDatagram(object sender, UDP.NetEventArgs e)
        {
            recvString = System.Text.Encoding.ASCII.GetString(e.Buff);
            Object[] list = { this, System.EventArgs.Empty };
            parent.getPictureBox().BeginInvoke(new EventHandler(parent.pauseString), list); 
            //parent.pauseString(text);
        }
    }
}
